package com.ffzx.parentweb.utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.alibaba.dubbo.rpc.service.GenericService;
import com.fasterxml.jackson.databind.JavaType;
import com.ffzx.commerce.framework.dto.ResultDto;
import com.ffzx.commerce.framework.utils.JsonMapper;
import com.ffzx.commerce.framework.utils.RedisWebUtils;
import com.ffzx.commerce.framework.utils.SingletonDubboApplication;
import com.ffzx.commerce.framework.utils.StringUtil;
import com.ffzx.commerce.framework.vo.SysMenuVo;

/**
 * PermisstionUtil
 * 
 * @author CMM
 *
 * 2016年3月29日 上午11:54:08
 */
@Component(value = "permisstionUtil")
public class PermisstionUtil {
	
	private static Logger logger = LoggerFactory.getLogger(PermisstionUtil.class);
	
	private GenericService sysPermissionApiService;
	
	{
		ReferenceBean<GenericService> referenceConfig = new ReferenceBean<GenericService>();
		referenceConfig.setInterface("com.ffzx.permission.api.service.SysPermissionApiService");
		referenceConfig.setGeneric(Boolean.TRUE);
		referenceConfig.setVersion("1.0");
		referenceConfig.setId("sysPermissionApiService");
		SingletonDubboApplication.getInstance().destroyBean(referenceConfig.getId());
		SingletonDubboApplication.getInstance().addReferenceBean(referenceConfig.getId(), referenceConfig);
		sysPermissionApiService = (GenericService) SingletonDubboApplication.getInstance().getReferenceObject(referenceConfig.getId());
	}
	
	
	/**
	 * 初始化权限
	 * 
	 * @param session
	 */
	@SuppressWarnings("unchecked")
	public void initPermission(String userId) {
		Set<String> permission = new HashSet<String>();
		try {
			Map<?, ?> genericResult = (Map<?, ?>) sysPermissionApiService.$invoke("getFunctionPermissionList", new String[] { "java.lang.String" }, new Object[] { userId });
			ResultDto resultDto = new ResultDto();
			resultDto.setCode((String)genericResult.get("code"));
			resultDto.setMessage((String)genericResult.get("message"));
			resultDto.setData(genericResult.get("data"));
			
			if (resultDto != null) {
				List<String> list = (List<String>)resultDto.getData();
				if (StringUtil.isNotNull(list)) {
					permission.addAll(list);
				}
			}
		} catch (Exception e) {
			logger.error("PermisstionUtil-initPermission-Exception=》-初始化用户权限-Exception", e);
		}
		
		//缓存到redis
		RedisWebUtils.setPermission(permission);
	}
	
	/**
	 * 获取权限
	 * 
	 * @param userId
	 * @return Set<String>
	 */
	public Set<String> getPermission(String userId) {
		Set<String> personPermission = RedisWebUtils.getPermission();
		if (!StringUtil.isNotNull(personPermission)) {
			initPermission(userId);
			personPermission = RedisWebUtils.getPermission();
		}
		return personPermission;
	}
	
	
	/**
	 * 用户id获取菜单 返回ResultDto对象,ResultDto.data 格式为List<SysMenu>
	 * 
	 * @param userId
	 * @return {@link ResultDto}
	 */
	public ResultDto getMenuPermissionList(String userId) {
		ResultDto rsDto = new ResultDto(ResultDto.OK_CODE, "success");
		
		Map<?, ?> genericResult = (Map<?, ?>) sysPermissionApiService.$invoke("getMenuPermissionList", new String[] { "java.lang.String" }, new Object[] { userId });
		ResultDto resultDto = new ResultDto();
		resultDto.setCode((String)genericResult.get("code"));
		resultDto.setMessage((String)genericResult.get("message"));
		
		JsonMapper jsonMapper = JsonMapper.getInstance();
		JavaType javaType = jsonMapper.createCollectionType(ArrayList.class, SysMenuVo.class);
		List<SysMenuVo> list = jsonMapper.fromJson((String)genericResult.get("data"), javaType);
		rsDto.setData(list);
		return rsDto;
	}

}
